% Estimate the phases from the slope of the apparent resistivity curve
% basically from automod 1d ivnversion code
% test 
% tested with Puga (Best!) data- not really good -9/11/02
% When solpe of rho curve is small, phase is erratic
% latest date 09.11.2002


function[phi,er]=rh2p(rho,t)
er=1;

kk = length(t);

if kk < 2,
er=-1;
return;
end;

tk1=log10(t(2)/t(1));
tk2=log10(t(3)/t(2));
tkp=tk1+tk2;
pi4=pi/4;
phi(1)=pi4*(1+(tk2+2*tk1)/(tk1*tkp)*log10(rho(1))...
	+tk1/(tk2*tkp)*log10(rho(3))-tkp/(tk1*tk2)*log10(rho(2)));
tk1=log10(t(kk)/t(kk-1));
tk2=log10(t(kk-1)/t(kk-2));
tkp=tk1+tk2;
phi(kk)=pi4*(1-(tk2+2*tk1)/(tk1*tkp)*log10(rho(kk))...
	-tk1/(tk2*tkp)*log10(rho(kk-2))+tkp/(tk1*tk2)*log10(rho(kk-1)));
for k = 2:kk-1,
tk1=log10(t(k)/t(k-1));
tk=log10(t(k+1)/t(k));
tkp=tk1+tk2;
phi(k)=pi4*(1-tk1/(tk*tkp)*log10(rho(k+1))...
	+tk/(tk1*tkp)*log10(rho(k-1))+(tk1-tk)/(tk1*tk)*log10(rho(k)));
end;


